package xlj.lib.android.base.utils;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.support.v4.content.FileProvider;
import android.text.TextUtils;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;

import java.io.File;

public class Util {

    /**
     *
     * @param url String
     * @return boolean
     */
    public static boolean syncCookie(Context context, String url, String token) {
        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.setAcceptCookie(true);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            cookieManager.removeSessionCookie();
            cookieManager.setCookie(url, "token=" + token);
            CookieSyncManager.getInstance().sync();
        } else {
            cookieManager.removeSessionCookies(null);
            cookieManager.setCookie(url, "token=" + token);
            cookieManager.flush();
        }
        String newCookie = cookieManager.getCookie(url);
        return !TextUtils.isEmpty(newCookie);
    }

    public static int getVersionCode(Context context) {
        PackageManager packageManager = context.getPackageManager();
        PackageInfo packageInfo = null;
        try {
            packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        if (packageInfo != null) {
            String mVersionName = packageInfo.versionName;
            int mVersionCode = packageInfo.versionCode;
            return mVersionCode;
        }
        return 0;

    }

    /**
     * 获取版本信息
     * @param context
     * @return  version_name
     */
    public static String getVersionName(Context context) {
        PackageManager packageManager = context.getPackageManager();
        PackageInfo packageInfo = null;
        try {
            packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        if (packageInfo != null) {
            return packageInfo.versionName;
        }
        return "";

    }

    /**
     * 安装apk
     * @param context
     * @param fileApk
     */
    public static void install(Context context,File fileApk) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            Uri contentUri = FileProvider.getUriForFile(
                    context
                    , "hongcaosp.app.android.fileProvider"
                    , fileApk);
            intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
        } else {
            intent.setDataAndType(Uri.fromFile(fileApk), "application/vnd.android.package-archive");
        }
        if (context != null) {
            context.startActivity(intent);
//            android.os.Process.killProcess(android.os.Process.myPid());
        }
    }
}
